package com.xs.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xs.domain.Staff;
import com.xs.domain.dto.StaffCollectDTO;
import com.xs.domain.dto.StaffDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author admin
 * @description 针对表【staff(乐谱)】的数据库操作Mapper
 * @createDate 2023-01-11 17:02:39
 * @Entity com.xs.domain.Staff
 */
@Mapper
public interface StaffMapper extends BaseMapper<Staff> {

    @Select("SELECT staff.name,staff.consumer_id,staff.id,staff.type,staff.introduction,staff.picture,view_counts,staff.file,staff_list.title,staff_list.style,staff_list.id as staffListId,list_staff.id AS listStaffId FROM list_staff JOIN staff ON list_staff.staff_id=staff.id JOIN staff_list ON list_staff.staff_list_id=staff_list.id WHERE staff.consumer_id=#{consumerId}")
    Page<StaffDTO> selectUpdateStaffByConsumerId(Page<StaffDTO> page, Integer consumerId);
    /**
     * 根据乐谱集id查找乐谱
     *
     * @param page
     * @param staffListId
     * @return
     */
    @Select("SELECT staff.id,staff.name,staff.introduction,staff.picture,staff.file,view_counts FROM list_staff " +
            "JOIN staff ON list_staff.staff_id=staff.id " +
            "JOIN staff_list ON list_staff.staff_list_id=staff_list.id " +
            "WHERE staff_list.id=#{staffListId} and staff.name LIKE '%${name}%' and staff.type=0")
    Page<StaffDTO> selectStaff(Page<StaffDTO> page, Integer staffListId, String name);

    /**
     * 根据乐谱集id查找乐谱不分页
     *
     * @param staffListId
     * @return
     */
    @Select("SELECT staff.id,staff.consumer_id,staff.name,staff.introduction,staff.file,staff.picture,view_counts,staff.create_time,staff.file FROM list_staff " +
            "JOIN staff ON list_staff.staff_id=staff.id " +
            "JOIN staff_list ON list_staff.staff_list_id=staff_list.id " +
            "WHERE staff_list.id=#{staffListId} and staff.type =0")
    List<StaffDTO> selectStaffs(Integer staffListId);

    /**
     * 查询乐谱总数
     * @return
     */
    @Select("SELECT COUNT(id) FROM staff")
    int staffCount();

    /**
     * 根据用户id查询该用户收藏的乐谱详情
     * @param consumerId
     * @return
     */
    @Select("SELECT staff.* FROM collect  JOIN staff on staff.id=collect.staff_id JOIN consumer ON consumer.id=collect.consumer_id WHERE consumer.id=#{consumerId}")
    List<StaffCollectDTO> likeStaff(Integer consumerId);


    /**
     * 根据用户id查询该用户收藏的乐谱详情分页
     * @param consumerId
     * @return
     */
    @Select("SELECT staff.* FROM collect  JOIN staff on staff.id=collect.staff_id JOIN consumer ON consumer.id=collect.consumer_id WHERE consumer.id=#{consumerId}")
    Page<StaffCollectDTO> likeStaffPage(Page<StaffCollectDTO> page,Integer consumerId);

    /**
     * 查询需要审批的用户
     * @return
     */
    @Update("UPDATE staff  set type=#{type} WHERE id=#{staffId}")
    int changeStaffType(Integer staffId,Integer type);
}
